package com.springboot.project.common.controller;

import com.springboot.config.shiro.ShiroTool;
import com.springboot.config.tool.ToolUtil;
import com.springboot.project.common.entity.Log;
import com.springboot.project.common.entity.User;
import com.springboot.project.common.node.ModuleNode;
import com.springboot.project.common.service.impl.LogServiceImpl;
import com.springboot.project.common.service.impl.ModuleServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

import static org.springframework.util.StringUtils.isEmpty;

/**
 * Created with IntelliJ IDEA.
 * User: lby.
 * Date: 2018/4/1.
 * Version: V1.0.
 */
@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    ModuleServiceImpl moduleService;
    @Autowired
    private LogServiceImpl logService;

    @RequestMapping("/login")
    public String login(Model model) {
        if(ShiroTool.isLogin())
        {
            User user = (User) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
            //构建权限菜单树
            List<ModuleNode> moduleList =  moduleService.getModuleListByRoles(user.getRoleid());
            List<ModuleNode> titles = ModuleNode.buildTitle(moduleList);
            model.addAttribute("titles", titles);
            model.addAttribute("user", user);
            return "index";
        }
        return "login";
    }

    @RequestMapping("/loginUser")
    public String loginUser(String username,String password,Model model,HttpServletRequest request) {
        if(isEmpty(username) || isEmpty(password))
        {
            return "redirect:/login";
        }
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(username,password.toCharArray());
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(usernamePasswordToken);   //完成登录
            User user = (User) SecurityUtils.getSubject().getPrincipals().getPrimaryPrincipal();
            //构建权限菜单树
            List<ModuleNode> moduleList =  moduleService.getModuleListByRoles(user.getRoleid());
            List<ModuleNode> titles = ModuleNode.buildTitle(moduleList);
            model.addAttribute("titles", titles);
            model.addAttribute("user", user);
            //添加登录日志
            Log log = new Log();
            log.setUserId((long)(user.getId()));
            log.setUsername(user.getUsername());
            log.setOperation("登录");
            log.setCreateTime(new Date());
            log.setType(3);
            log.setTime(0);
            log.setMethod("——");
            log.setDetail("登录");
            log.setIp(ToolUtil.getIpAddr(request));
            logService.insert(log);
            //返回主页
            return "index";
        } catch(Exception e) {
            System.err.println("------出错------");
            return "login";//返回登录页面
        }
    }

    @RequestMapping("/logOut")
    public String logOut(HttpSession session) {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return "login";
    }

    @RequestMapping("/homepage")
    public String homepage() {
        return "homepage";
    }

}
